<template>
  <div class="newslist-contain">
    <van-loading size="24px" vertical v-if="isLoading">加载中...</van-loading>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <ul>
        <li
          v-for="item in newslist"
          :key="item.id"
          @click="$router.push(`/newsparticulars/${item.id}`)"
        >
          <img :src="item.img_url" />
          <div class="text">
            <div class="title">{{ item.title }}</div>
            <div class="explain">
              <span
                >发布时间：
                {{ $momentFun(item.add_time, 'YYYY-MM-DD') }}
              </span>
              <span>点击次数：{{ item.click }}</span>
            </div>
          </div>
        </li>
      </ul>
    </van-pull-refresh>
    <van-divider v-if="isLoadDone">已经全部加载</van-divider>
    <!-- <div>
      <van-button type="info" @click="load">加载</van-button>
    </div> -->
  </div>
</template>

<script setup>
import { getNewsList } from '@/api';
import { ref, onMounted, onBeforeUnmount } from 'vue';
const page = ref(1);
const pagesize = ref(10);
const newslist = ref([]);
const isLoading = ref(false);
const isLoadDone = ref(false);

onMounted(() => {
  console.log('hhhh');
  GetNewsList();
  window.addEventListener('scroll', loadGoodsList);
});

const GetNewsList = async () => {
  let { message } = await getNewsList(page.value, pagesize.value);
  newslist.value = message;
};

const onRefresh = async () => {
  isLoading.value = true;
  page.value = 1;
  newslist.value = [];
  let { message } = await getNewsList(page.value, pagesize.value);
  isLoading.value = false;
  newslist.value = message;
};

const loadGoodsList = async () => {
  if (isLoadDone.value) {
    return;
  }
  //滚上去的高度
  let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  //   网页视口的宽度
  let windowHeight =
    document.documentElement.clientHeight || document.body.clientHeight;
  //整个页面的高度
  let pageHeight =
    document.documentElement.scrollHeight || document.body.scrollHeight;
  if (Math.ceil(scrollTop) + windowHeight === pageHeight) {
    page.value++;
    let { message } = await getNewsList(page.value, pagesize.value);
    if (message.length === 0) {
      isLoadDone.value = true;
      return;
    }
    newslist.value = [...newslist.value, ...message];
  }

  onBeforeUnmount(() => {
    window.removeEventListener('scroll', loadGoodsList);
  });
};
</script>

<style lang="scss" scoped>
.newslist-contain {
  padding: 10px;

  .van-button {
    width: 100%;
  }

  ul {
    li {
      display: flex;
      justify-content: space-between;
      width: 100%;
      height: 104px;
      padding-top: 3px;
      border-bottom: 1px solid #ccc;

      img {
        width: 100px;
        height: 100%;
      }

      .text {
        display: flex;
        flex: 1;
        flex-direction: column;
        justify-content: space-between;
        padding: 10px 5px;

        .title {
          font-size: 14px;
          font-weight: 700;
        }

        .explain {
          display: flex;
          justify-content: space-between;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
